package com.note.dao.user;

import com.note.pojo.Users;
import com.note.utils.BaseCC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class userDaoImpl implements userDao{
    @Override
    public Users loginDao(String username, String password) throws SQLException {
        Connection conn = BaseCC.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from users where username=? and password=?";
        Object[] params = {username, password};
        rs = BaseCC.execute(conn, sql, params, ps, rs);
        Users user = new Users();
        if(rs.next()){
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setNickname(rs.getString("nickname"));
            user.setMail(rs.getString("mail"));
        }
        BaseCC.closeResource(conn, ps, rs);
        return user;
    }

    @Override
    public int updatePasswd(String password, int id) throws SQLException {
        Connection conn = BaseCC.getConnection();
        PreparedStatement ps = null;
        String sql = "update users set password=? where id=?";
        Object[] params = {password, id};
        int i = BaseCC.execute(conn, sql, params, ps);
        BaseCC.closeResource(conn, ps, null);
        return i;
    }

    @Override
    public int updateName(String password, int id) throws SQLException {
        Connection conn = BaseCC.getConnection();
        PreparedStatement ps = null;
        String sql = "update users set nickname=? where id=?";
        Object[] params = {password, id};
        int i = BaseCC.execute(conn, sql, params, ps);
        BaseCC.closeResource(conn, ps, null);
        return i;
    }
}
